home *** CD-ROM | disk | FTP | other *** search
/ Programming Microsoft Visual Basic .NET / Programming Microsoft Visual Basic .NET (Microsoft Press)(X08-78517)(2002).bin / 05 inheritance / inheritancedemo / module1.vb < prev   
Encoding:
Text File  |  2002-03-20  |  8.6 KB  |  262 lines

  1. Module MainModule
  2.  
  3.     Sub Main()
  4.         ' Run one of the Textxxxx procedures below by uncommenting only one statement
  5.  
  6.         'TestInheritance()
  7.         'TestInheritance2()
  8.         'TestInheritance3()
  9.         'TestInheritance4()
  10.         'TestOverridablePerformance()
  11.         'TestMyClassKeyword()
  12.         'TestMemberShadowing()
  13.         'TestShadows()
  14.         'TestShadows2()
  15.         'TestProtectedScope()
  16.         'TestProtectedScope2()
  17.         'TestTrapEvents()
  18.         'TestTrapEvents2()
  19.  
  20.         ' You need these statements when running inside Visual Studio, so that
  21.         ' the Console window doesn't disappear
  22.         Console.WriteLine("")
  23.         Console.WriteLine(">>> press Enter to terminate the program <<<")
  24.         Console.ReadLine()
  25.  
  26.     End Sub
  27.  
  28.     ' This procedure test basic inheritance
  29.  
  30.     Sub TestInheritance()
  31.         Dim e As New Employee()
  32.         e.FirstName = "Joe"
  33.         e.LastName = "Doe"
  34.         ' This assignment always works.
  35.         Dim p As Person = e
  36.         ' This proves that p points to the Employee object.
  37.         Console.WriteLine(p.CompleteName)    '=> Joe Doe
  38.     End Sub
  39.  
  40.     ' This procedure demonstrate event inheritance.
  41.  
  42.     Dim WithEvents anEmployee As Employee
  43.  
  44.     Sub TestInheritance2()
  45.         ' Create the event sink.
  46.         anEmployee = New Employee()
  47.         anEmployee.FirstName = "Joe"
  48.         anEmployee.LastName = "Doe"
  49.  
  50.         ' Notify this employee that he got new mail.
  51.         ' (This indirectly raises the event.)
  52.         anEmployee.NotifyNewMail("Message from VB2TheMax")
  53.     End Sub
  54.  
  55.     ' The event procedure 
  56.     Sub Employee_NewMail(ByVal msgText As String) Handles anEmployee.GotMail
  57.         Console.WriteLine("NEW MAIL: " & msgText)
  58.     End Sub
  59.  
  60.     ' this procedure demonstrates shared member inheritance.
  61.  
  62.     Sub TestInheritance3()
  63.         Dim e1 As New Employee()
  64.         e1.FirstName = "Joe"
  65.         e1.LastName = "Doe"
  66.  
  67.         Dim e2 As New Employee()
  68.         e2.FirstName = "Robert"
  69.         e2.LastName = "Doe"
  70.  
  71.         Dim e3 As New Employee()
  72.         e3.FirstName = "Ann"
  73.         e3.LastName = "Doe"
  74.  
  75.         ' Joe is Robert's and Ann's father.
  76.         e2.Father = e1
  77.         e3.Father = e1
  78.         ' Call the inherited shared method in the Employee class.
  79.         Console.WriteLine(Employee.AreBrothers(e2, e3))    ' => True
  80.     End Sub
  81.  
  82.     ' this procedure tests derived classes with non-default constructors
  83.  
  84.     Sub TestInheritance4()
  85.         ' create an Employee without a Title.
  86.         Dim e1 As New Employee2("Joe", "Doe")
  87.         Console.WriteLine(e1.CompleteName)     ' => Joe Doe
  88.  
  89.         ' create an Employee with a Title.
  90.         Dim e2 As New Employee2("Joe", "Doe", "Mr.")
  91.         Console.WriteLine(e2.CompleteName)     ' => Mr. Joe Doe
  92.     End Sub
  93.  
  94.     ' this procedure benchmarks NotOverridable and Overridable methods
  95.     ' run this code with Start without Debugging (Ctrl+F5) to see the
  96.     ' best results.
  97.  
  98.     Sub TestOverridablePerformance()
  99.         Const Times As Integer = 200000000
  100.  
  101.         Dim t As Date
  102.         Dim i As Integer
  103.         Dim sum As Integer
  104.         Dim w As New Widget()
  105.  
  106.         ' benchmark calls to non-overridable method.
  107.         t = Now
  108.         For i = 1 To Times
  109.             sum += w.GetInteger
  110.         Next
  111.         Console.WriteLine("Non-overridable method: {0} secs.", Now.Subtract(t))
  112.  
  113.         ' benchmark calls to overridable method.
  114.         t = Now
  115.         For i = 1 To Times
  116.             sum += w.GetInteger2
  117.         Next
  118.         Console.WriteLine("Overridable method: {0} secs.", Now.Subtract(t))
  119.  
  120.         ' benchmark calls to a method in the class interface.
  121.         t = Now
  122.         For i = 1 To Times
  123.             w.Dispose()
  124.         Next
  125.         Console.WriteLine("Method in class interface: {0} secs.", Now.Subtract(t))
  126.  
  127.         ' benchmark calls to a method in an interface.
  128.         Dim d As IDisposable = CType(w, IDisposable)
  129.         t = Now
  130.         For i = 1 To Times
  131.             d.Dispose()
  132.         Next
  133.         Console.WriteLine("Method in an interface: {0} secs.", Now.Subtract(t))
  134.     End Sub
  135.  
  136.     ' this procedure shows which kind of problem you can solve with the MyClass keyword.
  137.  
  138.     Sub TestMyClassKeyword()
  139.         Dim e As New Employee3("Joe", "Doe")
  140.         e.Gender = Gender.Male
  141.         ' the TitledName method in Person3 (base class) uses the 
  142.         ' overridden version of Title property in Employee3 (derived class).
  143.         Console.WriteLine(e.TitledName)     ' => Mr. Joe Doe
  144.  
  145.         ' Create a person and an employee.
  146.         Dim p2 As New Person3("Joe", "Doe")
  147.         Dim e2 As New Employee3("Robert", "Smith")
  148.         ' They are born on the same day.
  149.         p2.BirthDate = #12/31/1983#
  150.         e2.BirthDate = #12/31/1983#
  151.         ' (Assuming that you run this code in year 2001...)
  152.         ' The person can't vote yet (correct).
  153.  
  154.         Console.WriteLine(p2.CanVote)          ' => False
  155.         ' The employee appears to be allowed to vote (incorrect).
  156.         Console.WriteLine(e2.CanVote)          ' => True
  157.  
  158.         ' the CanVote2 procedure returns correct values because it relies on the MyClass keyword
  159.         Console.WriteLine(e2.CanVote2)         ' => False
  160.     End Sub
  161.  
  162.     ' this procedure tests member shadowing
  163.  
  164.     Sub TestMemberShadowing()
  165.         Dim b As New BBB()
  166.         b.DoSomething()
  167.         b.DoSomething("abc")
  168.         b.DoSomething2()
  169.         b.DoSomething2("abc")
  170.     End Sub
  171.  
  172.     ' this procedure tests the Shadows keyword
  173.  
  174.     Sub TestShadows()
  175.         ' create a Person3 object
  176.         Dim p As New Person3("Joe", "Doe")
  177.         ' show that you can assign a null string to its Address property
  178.         ' without raising any error
  179.         p.Address = ""
  180.  
  181.         ' create an Employee object 
  182.         Dim e As New Employee3("Ann", "Doe")
  183.         ' demonstrates that the Employee overrides the (non-overridable) Address property.
  184.         Try
  185.             ' NOTE: next statement throws an exception: this proves that the Employee3 class
  186.             '       has overridden the Address property to provide a more robust implementation.
  187.             e.Address = ""
  188.         Catch ex As Exception
  189.             Console.WriteLine("An exception occurred: " & ex.Message)
  190.         End Try
  191.     End Sub
  192.  
  193.     Sub TestShadows2()
  194.         Dim e As New Employee3("Joe", "Doe")
  195.  
  196.         Try
  197.             ' This statement correctly raises an ArgumentException, 
  198.             ' because of the code in the Employee class
  199.             e.Address = ""
  200.         Catch ex As Exception
  201.             Console.WriteLine("An exception occurred: " & ex.Message)
  202.         End Try
  203.  
  204.         ' Access the same object through a base class variable.
  205.         Dim p As Person3 = e
  206.         ' This raises no runtime error, because the Address property procedure
  207.         ' in the base class is actually executed.
  208.         p.Address = ""
  209.     End Sub
  210.  
  211.     ' these procedures demonstate the Protected keyword.
  212.  
  213.     Sub TestProtectedScope()
  214.         Dim c1 As New Customer()
  215.         Dim c2 As New GoodCustomer()
  216.         Console.WriteLine(c1.TotalOrderAmount(10000, 100))   ' => 9100
  217.         Console.WriteLine(c2.TotalOrderAmount(10000, 100))   ' => 8600
  218.     End Sub
  219.  
  220.     ' this procedure tests the Protected scope
  221.  
  222.     Sub TestProtectedScope2()
  223.         Dim c3 As New ForeignCustomer(False)    ' ill-behaved
  224.         Dim c4 As New ForeignCustomer(True)     ' well-behaved
  225.  
  226.         Console.WriteLine(c3.TotalOrderAmount(10000, 400))   ' => 9400
  227.         Console.WriteLine(c4.TotalOrderAmount(10000, 400))   ' => 8500
  228.     End Sub
  229.  
  230.     ' this procedure shows how a derived class can trap events defined in its base class
  231.  
  232.     Dim WithEvents fdr As New FileDataReader()
  233.  
  234.     Sub TestTrapEvents()
  235.         ' cause a DataAvailable event to be fired - this event is fired in the next procedure
  236.         fdr.GetNewData()
  237.     End Sub
  238.  
  239.     Public Sub DataAvailableEvent() Handles fdr.DataAvailable
  240.         Console.WriteLine("DataAvailable event")
  241.     End Sub
  242.  
  243.     ' this procedure shows how a derived class can trap AND control events defined
  244.     ' in its base class
  245.  
  246.     Dim WithEvents fdr2 As New FileDataReader2()
  247.  
  248.     Sub TestTrapEvents2()
  249.         ' prove that the derived class can prevent the base class from rising the event
  250.         Dim i As Integer
  251.         ' only the first 10 GetNewDate methods raise a DataAvailable event
  252.         For i = 1 To 20
  253.             fdr2.GetNewData()
  254.         Next
  255.     End Sub
  256.  
  257.     Public Sub DataAvailableEvent2() Handles fdr2.DataAvailable
  258.         Console.WriteLine("DataAvailable event #" & CStr(fdr2.EventCounter))
  259.     End Sub
  260. End Module
  261.  
  262.